package com.example.springboot.controller;

import com.example.springboot.service.EarningsService;
import com.example.springboot.entity.Earnings;
import com.github.pagehelper.PageInfo;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.math.BigDecimal;

@RestController
@RequestMapping("/earnings")
public class EarningsController {
    private final EarningsService earningsService;

    public EarningsController(EarningsService earningsService) {
        this.earningsService = earningsService;
    }

    @GetMapping("/merchant/{merchantId}")
    public List<Earnings> getMerchantEarnings(@PathVariable int merchantId) {
        return earningsService.getEarningsByMerchantId(merchantId);
    }

    @GetMapping("/admin/{adminId}/earnings")
    public List<Earnings> getAdminEarnings(@PathVariable int adminId) {
        return earningsService.getEarningsByAdminId(adminId);
    }


    @GetMapping("/all")
    public List<Earnings> getAllEarnings() {
        return earningsService.getAllEarnings();
    }

    @GetMapping("/merchant/total/{merchantId}")
    public BigDecimal getTotalEarningsByMerchant(@PathVariable int merchantId) {
        return earningsService.getTotalEarningsByMerchantId(merchantId);
    }

        @GetMapping("/platform/total")
        public BigDecimal getTotalPlatformEarnings() {
            return earningsService.getTotalPlatformEarnings();
        }

        @GetMapping("/merchant/total")
        public BigDecimal getTotalMerchantEarnings() {
            return earningsService.getTotalMerchantEarnings();
        }
    @GetMapping("/page")
    public PageInfo<Earnings> getEarningsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return earningsService.getEarningsByPage(pageNum, pageSize);
    }

    @GetMapping("/filter")
    public List<Earnings> filterEarnings(
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @RequestParam(required = false) String orderType) {
        return earningsService.filterEarnings(startTime, endTime, orderType);
    }

    @GetMapping("/platform/total/range")
    public BigDecimal getPlatformTotalByDateRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date start,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end) {
        return earningsService.getPlatformTotalByDateRange(start, end);
    }

    @GetMapping("/admin/{adminId}/total")
    public BigDecimal getTotalByAdmin(@PathVariable int adminId) {
        return earningsService.getTotalEarningsByAdmin(adminId);
    }

    @GetMapping("/self-service/admin/{adminId}")
    public BigDecimal getSelfServiceEarningsByAdmin(@PathVariable int adminId) {
        return earningsService.getSelfServiceEarningsByAdmin(adminId);
    }
    @GetMapping("/admin/{adminId}/platform/total")
    public BigDecimal getPlatformTotalByAdmin(@PathVariable int adminId) {
        return earningsService.getPlatformTotalByAdmin(adminId);
    }
    @GetMapping("/admin/{adminId}/merchant/total")
    public BigDecimal getMerchantTotalByAdmin(@PathVariable int adminId) {
        return earningsService.getMerchantTotalByAdmin(adminId);
    }



}
